﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using KoolearnSample.Model;
using KoolearnSample.ViewModel;
using Windows.UI.ViewManagement;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace KoolearnSample.Views.Open
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Demo : Page
    {
        #region Members
        private JsonOper Oper;
        #endregion

        #region Construction
        public Demo()
        {
            this.InitializeComponent();
            Oper = new JsonOper();
            Window.Current.SizeChanged += (s, e) =>
            {
                var state = ApplicationView.Value;
                switch (state)
                {
                    case ApplicationViewState.Snapped:
                        VisualStateManager.GoToState(this, "Snapped", true);
                        break;
                    default:
                        VisualStateManager.GoToState(this, "LanscapeFullScreen", true);
                        break;
                }
            };
        }
        #endregion

        #region Method
        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            //获取公开课的分类
            await Oper.GetDataAsync<CategorysViewModel>(ApiAddress.GetOpenCategoryAddress()
                , t =>
                {
                    PopupLoading.IsOpen = false;
                    this.DataContext = t;
                });
        }
        //根据公开课分类编号获取课程列表
        private async void ListCategory_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            PopupLoading.IsOpen = true;
            var url = string.Format(ApiAddress.GetOpenCourseAddress(),(e.ClickedItem as KoolearnCategory).Id);
            await Oper.GetDataAsync<CoursesViewModel>(url,
                t =>
                {
                    PopupLoading.IsOpen = false;
                    GridCourses.DataContext = t;
                });
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Frame.GoBack();
        }
        #endregion
    }
}
